package com.elai.operate.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.StringUtils;
import com.elai.operate.domain.OperatePalnDO;
import com.elai.operate.domain.OperatePlanDO;
import com.elai.operate.mapper.OperatePlanMapper;
import com.elai.operate.service.OperatePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
public class OperatePlanServiceImpl implements OperatePlanService {

    @Autowired
    private OperatePlanMapper operatePlanMapper;

    @Override
    public int insertOperatePlan(OperatePlanDO operatePlan) {
        operatePlan.setCreateTime(DateUtils.getNowDate());
        operatePlan.setCheckStatus("01");
        OperatePlanDO new0peratePlan=extractedJson(operatePlan);
        return operatePlanMapper.insertOperatePlan(new0peratePlan);
    }

    @Override
    public List<OperatePlanDO> selectOperatePlanList(OperatePlanDO operatePlan) {
        return operatePlanMapper.selectOperatePlanList(operatePlan);
    }

    private OperatePlanDO extractedJson(OperatePlanDO operatePlan) {
        String planTypeIndex = operatePlan.getPlanTypeIndex();
        JSONObject a = new JSONObject();
        //第一步
        JSONObject step1 = new JSONObject();
        JSONObject step2 = new JSONObject();
        JSONObject step3 = new JSONObject();
        step1.put("name", operatePlan.getPlanName());
        step1.put("planTypeIndex", operatePlan.getTitle());
        step1.put("triggerTypeIndex", operatePlan.getTriggerTypeIndex());
        if ("1".equals(planTypeIndex)) {
            step1.put("triggerDate",operatePlan.getTriggerDate());
            step1.put("triggerDateTime",operatePlan.getTriggerDateTime());
        } else if ("2".equals(planTypeIndex)) {
            step1.put("frequency",operatePlan.getFrequency());
            step1.put("triggerTime",operatePlan.getTriggerTime());
            step1.put("startDate",operatePlan.getStartDate());
            step1.put("startTime",operatePlan.getStartTime());
            step1.put("endDate",operatePlan.getEndDate());
            step1.put("endTime",operatePlan.getEndTime());
            step1.put("limitType",operatePlan.getLimitType());
            step1.put("limitTimeSlot",operatePlan.getLimitTimeSlot());
            step1.put("limitTimes",operatePlan.getLimitTimes());
        } else if ("3".equals(planTypeIndex)) {
            step1.put("eventA",operatePlan.getEventA());
            step1.put("reachTimeType",operatePlan.getReachTimeType());
            step1.put("reachTimeType",operatePlan.getReachTimeType());
            step1.put("startDate",operatePlan.getStartDate());
            step1.put("startTime",operatePlan.getStartTime());
            step1.put("endDate",operatePlan.getEndDate());
            step1.put("endTime",operatePlan.getEndTime());
            step1.put("limitType",operatePlan.getLimitType());
            step1.put("limitTimeSlot",operatePlan.getLimitTimeSlot());
            step1.put("limitTimes",operatePlan.getLimitTimes());
        } else if ("4".equals(planTypeIndex)) {
            step1.put("eventA",operatePlan.getEventA());
            step1.put("timeSlotNum",operatePlan.getTimeSlotNum());
            step1.put("timeSlotUnit",operatePlan.getTimeSlotUnit());
            step1.put("eventB",operatePlan.getEventB());
            step1.put("reachTimeType",operatePlan.getReachTimeType());
            step1.put("reachTimeType",operatePlan.getReachTimeType());
            step1.put("startDate",operatePlan.getStartDate());
            step1.put("startTime",operatePlan.getStartTime());
            step1.put("endDate",operatePlan.getEndDate());
            step1.put("endTime",operatePlan.getEndTime());
            step1.put("limitType",operatePlan.getLimitType());
            step1.put("limitTimeSlot",operatePlan.getLimitTimeSlot());
            step1.put("limitTimes",operatePlan.getLimitTimes());
        }
        operatePlan.setStep1Json(step1.toString());
        //第二步
        step2.put("userAttributes",operatePlan.getUserAttributes());
        step2.put("userActions",operatePlan.getUserActions());
        step2.put("userActionOrders",operatePlan.getUserActionOrders());
        operatePlan.setStep2Json(step2.toString());
        //第三步
        step3.put("supportABTest",operatePlan.getSupportABTest());
        step3.put("touch",operatePlan.getTouch());
        step3.put("touchType",operatePlan.getTouchType());
        step3.put("tickets",operatePlan.getTickets());
        step3.put("message",operatePlan.getMessage());
        step3.put("servercePlatform",operatePlan.getServercePlatform());
        step3.put("theme",operatePlan.getTheme());
        step3.put("content",operatePlan.getContent());
        step3.put("tempateId",operatePlan.getTempateId());
        step3.put("activityId",operatePlan.getActivityId());
        step3.put("doNotDisturb",operatePlan.getDoNotDisturb());
        step3.put("notDisturbEndTime",operatePlan.getNotDisturbEndTime());
        step3.put("action",operatePlan.getAction());
        operatePlan.setStep3Json(step3.toString());

        return  operatePlan;
    }



}
